/*
* Copyright (c) 2021, 深圳市光丽海科技有限公司
* All rights reserved.
*
* 文件名称：glh_app_process.c
* 文件标识：
* 摘 要：
*   主要对私有化的任务做处理
* 当前版本：V1.0
* 作 者： raoxiongjian
* 完成日期：2021年7月6日
*/
#include "glh_pwm.h"
#include "glh_app_process.h"
#include "glh_sys_tick.h"
#include "glh_led_mode.h"
#include "ty_beacon.h"
#include "hal_storage_256.h"
#include "ble_viot.h"
#include "int.h"

#define SC_CONFIG_ADDR 		     56
#define SC_CONFIG_ADDR2 		 112
#define SC_CONFIG_ADDR3 		 168
#define FLASH_WRITE_FLAG         0x55             //写入过数据的标志
#define SAVE_INTERVER            500
#define R_INDEX                  0
#define G_INDEX                  1
#define B_INDEX                  2
#define W_INDEX                  3
#define APS_DEFAULT_SPEED        90
#define ADDR_NUM_MAX             4
#define EMPTY_ADDR               0
#define APS_BN_MAX               255
#define APS_BN_MIN               25
#define APS_BN_CHANGE            26
#define APS_SPEED_MAX            100
#define APS_SPEED_MIN            1
#define APS_SPEED_CHANGE         ((APS_SPEED_MAX - APS_SPEED_MIN) >> 3)
#define R_MAX_POSITION_WHEEL_NUM        0
#define B_MAX_POSITION_WHEEL_NUM        64
#define G_MAX_POSITION_WHEEL_NUM        128
#define WHEEL_NUM_MAX                   255
#define NO_NEED_PAIR                    0x01
#define NEED_PAIR                       0x02
#define FACTORY_TEST_RSSI_OK_VALUE      180
#define RSSI_NUM                        8        //测试模式下需要采集几次的RSSI值来求平均值，从而判断是否合格
#define ON_OFF_SAVE_INTERVER            3000     //开关机后多长时间内不保存数据，防止开关机灯光渐变的过程中出现卡顿。

// 8键+圆环 beacon遥控器按键的名称类型
typedef enum _BEACON_COM_TYPE_E
{
	E_BEACON_COM_TYPE_ON = 0x02,
	E_BEACON_COM_TYPE_ON_LONG = 0x82,
	E_BEACON_COM_TYPE_OFF = 0x03,
	E_BEACON_COM_TYPE_OFF_LONG = 0x83,
    E_BEACON_COM_TYPE_COLOR_MODE_BN_INC = 0x05,
	E_BEACON_COM_TYPE_COLOR_MODE_BN_INC_LONG = 0x85,
    E_BEACON_COM_TYPE_COLOR_MODE_BN_DEC = 0x06,
	E_BEACON_COM_TYPE_COLOR_MODE_BN_DEC_LONG = 0x86,
    E_BEACON_COM_TYPE_MODE_INC = 0x14,
	E_BEACON_COM_TYPE_MODE_INC_LONG = 0x94,
    E_BEACON_COM_TYPE_MODE_DEC = 0x15,
	E_BEACON_COM_TYPE_MODE_DEC_LONG = 0x95,
    E_BEACON_COM_TYPE_SPEED_INC = 0x11,
	E_BEACON_COM_TYPE_SPEED_INC_LONG = 0x91,
    E_BEACON_COM_TYPE_SPEED_DEC = 0x12,
	E_BEACON_COM_TYPE_SPEED_DEC_LONG = 0x92,
	E_BEACON_COM_TYPE_COLOR_CIRCLE = 0x0a,
	E_BEACON_COM_TYPE_IS_NEED_PAIR = 0x7f,                    //工厂测试用的遥控器才可以发出这个命令
} BEACON_COM_TYPE_E;

typedef enum _MODE_TYPE_E
{
	E_MODE_TYPE_MIN = 0,
    E_MODE_TYPE_STATIC_COLOUR = 1,
	E_MODE_TYPE_STATIC_WHITE = 2,
	E_MODE_TYPE_STATIC_COLOUR_WHITE = 3,
	E_MODE_TYPE_FADE_RGB = 4,
	E_MODE_TYPE_BREATH_RGBW = 5,
	E_MODE_TYPE_JUMP_RGBW = 6,
	E_MODE_TYPE_FLASH_RGBYVCW_W_ALWAYS = 7,    //七彩跳变，白色始终跟着跳变。最后一种颜色就是只闪烁白色。
	E_MODE_TYPE_JUMP_RW = 8,
	E_MODE_TYPE_JUMP_BW = 9,
	E_MODE_TYPE_JUMP_GW = 10,
    E_MODE_TYPE_FLASH_R = 11,
    E_MODE_TYPE_FLASH_B = 12,
    E_MODE_TYPE_FLASH_G = 13,
    E_MODE_TYPE_FLASH_W = 14,
	E_MODE_TYPE_AUTO = 15,
    E_MODE_TYPE_MAX,
} MODE_TYPE_E;

#pragma pack(1)
typedef struct _SC_CONFIG_S
{
	uint8 u8CheckSum;
	uint8 u8CheckCrc;
	uint8 u8SaveFlag;
	BOOL bSwitch;
	uint8 u8Bn;
	uint8 u8Speed;
	uint8 au8Rgbw[4];
	MODE_TYPE_E eMode;
	uint32 au32Addr[ADDR_NUM_MAX];
	uint8 au8Group[ADDR_NUM_MAX];
	BOOL bIsNeedPair;
} SC_CONFIG_S;
#pragma pack()

static SC_CONFIG_S s_sScConfig, s_PreScConfig, s_ScConfigTemp;
static MODE_TYPE_E s_eModeAuto = E_MODE_TYPE_STATIC_COLOUR;  //用来记录自动模式执行到了哪一个模式
static BOOL s_bIsRgbControledByPair = FALSE;
static uint8 s_u8PairFlashCnt = 0;
static uint32 s_u32PairFlashTime = 0;
static uint8 s_u8PairFlashFlag = 0;
static uint32 s_u32PowerOnOffStartTime = 0 - ON_OFF_SAVE_INTERVER;     //关机开始的一段时间内部不去保存数据，防止等在逐渐熄灭的过程中出现卡顿。
static uint32 s_u32LastSaveTime = 0 - SAVE_INTERVER;


static void iWheelTransRgb(const uint8 u8Wheel, uint8 *pu8R, uint8 *pu8G, uint8 *pu8B);
static void iRgbPairFlashMainThread(void);

uint8 iGetCheckSum(uint8 *pData, uint8 u8Len)
{
	uint8 u8Ret = 0;
	uint8 i = 0;
	
	for(i=0; i<u8Len; i++)
	{
		u8Ret += pData[i];
	}
	
	return u8Ret;
}

unsigned char MyCrol(unsigned char u8Byte, unsigned char u8Cnt)
{
    unsigned char u8Temp = 0, i = 0;
    unsigned char u8Value = u8Byte;
    
    for(i=0; i<u8Cnt; i++)
    {
        u8Temp = ((u8Value & 0x80) >> 7);
        u8Value = (u8Value << 1) | u8Temp;
    }
    
    return u8Value;
}

uint8 iGetCheckCrc(uint8 *pData, uint8 u8Len)
{
	uint8 u8Ret = 0;
	uint8 i = 0;
	
	for(i=0; i<u8Len; i++)
	{
		u8Ret ^= MyCrol(pData[i], i % 8);
	}
	
	return u8Ret;
}

//复位模式参数
static void iResetScConfig(void)
{
	uint8 i = 0;
	
	s_sScConfig.bSwitch = ON;
	s_sScConfig.u8Bn = APS_BN_MAX;
	s_sScConfig.u8Speed = APS_DEFAULT_SPEED;
	s_sScConfig.au8Rgbw[R_INDEX] = COLOUR_MAX;
	s_sScConfig.au8Rgbw[G_INDEX] = 0;
	s_sScConfig.au8Rgbw[B_INDEX] = 0;
	s_sScConfig.au8Rgbw[W_INDEX] = 0;
	s_sScConfig.eMode = E_MODE_TYPE_STATIC_COLOUR;
	s_sScConfig.bIsNeedPair = TRUE;    //默认需要配对
	
	for(i=0; i<ADDR_NUM_MAX; i++)
	{
		s_sScConfig.au32Addr[i] = EMPTY_ADDR;
	}
}

static void iRefreshFromConfig(void)
{
    if(s_sScConfig.bSwitch == OFF)
	{
        GLHLM_PowerOff();
		return;
	}

    GLHLM_SetBn(s_sScConfig.u8Bn);
	GLHLM_SetSpeed(s_sScConfig.u8Speed);
	
	switch(s_sScConfig.eMode)
	{
		case E_MODE_TYPE_STATIC_COLOUR:
		{
			GLHLM_SetColorBuff(0, s_sScConfig.au8Rgbw[R_INDEX], s_sScConfig.au8Rgbw[G_INDEX], s_sScConfig.au8Rgbw[B_INDEX] , 0);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_STATIC_WHITE:
		{
			GLHLM_SetColorBuff(0, 0, 0, 0 , COLOUR_MAX);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_STATIC_COLOUR_WHITE:
		{
			GLHLM_SetColorBuff(0, s_sScConfig.au8Rgbw[R_INDEX], s_sScConfig.au8Rgbw[G_INDEX], s_sScConfig.au8Rgbw[B_INDEX] , COLOUR_MAX);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_FADE_RGB:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_FadeMode(3);
		}
		break;
		
		case E_MODE_TYPE_BREATH_RGBW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(3, 0, 0, 0 , COLOUR_MAX);
			GLHLM_BreathMode(4);
		}
		break;
		
		case E_MODE_TYPE_JUMP_RGBW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(3, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(4);
		}
		break;
		
		case E_MODE_TYPE_FLASH_RGBYVCW_W_ALWAYS:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(3, COLOUR_MAX, COLOUR_MAX, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(4, 0, COLOUR_MAX, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(5, COLOUR_MAX, 0, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(6, 0, 0, 0 , COLOUR_MAX);
			GLHLM_FlashMode(7);
		}
		break;
		
		case E_MODE_TYPE_JUMP_RW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_JUMP_BW:
		{
			GLHLM_SetColorBuff(0, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_JUMP_GW:
		{
			GLHLM_SetColorBuff(0, 0, COLOUR_MAX, 0, 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_FLASH_R:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_G:
		{
			GLHLM_SetColorBuff(0, 0, COLOUR_MAX, 0, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_B:
		{
			GLHLM_SetColorBuff(0, 0, 0, COLOUR_MAX, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_W:
		{
			GLHLM_SetColorBuff(0, 0, 0, 0, COLOUR_MAX);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_AUTO:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_FadeMode(3);
			s_eModeAuto = E_MODE_TYPE_FADE_RGB;
		}
		break;
		
		default:
		break;
	}
	
}

static void iAutoSlect(MODE_TYPE_E eAutoMode)
{
	switch(eAutoMode)
	{
		case E_MODE_TYPE_STATIC_COLOUR:
		{
			GLHLM_SetColorBuff(0, s_sScConfig.au8Rgbw[R_INDEX], s_sScConfig.au8Rgbw[G_INDEX], s_sScConfig.au8Rgbw[B_INDEX] , 0);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_STATIC_WHITE:
		{
			GLHLM_SetColorBuff(0, 0, 0, 0 , COLOUR_MAX);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_STATIC_COLOUR_WHITE:
		{
			GLHLM_SetColorBuff(0, s_sScConfig.au8Rgbw[R_INDEX], s_sScConfig.au8Rgbw[G_INDEX], s_sScConfig.au8Rgbw[B_INDEX] , COLOUR_MAX);
			GLHLM_StaticMode();
		}
		break;
		
		case E_MODE_TYPE_FADE_RGB:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_FadeMode(3);
		}
		break;
		
		case E_MODE_TYPE_BREATH_RGBW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(3, 0, 0, 0 , COLOUR_MAX);
			GLHLM_BreathMode(4);
		}
		break;
		
		case E_MODE_TYPE_JUMP_RGBW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(3, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(4);
		}
		break;
		
		case E_MODE_TYPE_FLASH_RGBYVCW_W_ALWAYS:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(3, COLOUR_MAX, COLOUR_MAX, 0 , COLOUR_MAX);
			GLHLM_SetColorBuff(4, 0, COLOUR_MAX, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(5, COLOUR_MAX, 0, COLOUR_MAX , COLOUR_MAX);
			GLHLM_SetColorBuff(6, 0, 0, 0 , COLOUR_MAX);
			GLHLM_FlashMode(7);
		}
		break;
		
		case E_MODE_TYPE_JUMP_RW:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_JUMP_BW:
		{
			GLHLM_SetColorBuff(0, 0, 0, COLOUR_MAX , 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_JUMP_GW:
		{
			GLHLM_SetColorBuff(0, 0, COLOUR_MAX, 0, 0);
			GLHLM_SetColorBuff(1, 0, 0, 0 , COLOUR_MAX);
			GLHLM_JumpMode(2);
		}
		break;
		
		case E_MODE_TYPE_FLASH_R:
		{
			GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_G:
		{
			GLHLM_SetColorBuff(0, 0, COLOUR_MAX, 0, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_B:
		{
			GLHLM_SetColorBuff(0, 0, 0, COLOUR_MAX, 0);
			GLHLM_FlashMode(1);
		}
		break;
		
		case E_MODE_TYPE_FLASH_W:
		{
			GLHLM_SetColorBuff(0, 0, 0, 0, COLOUR_MAX);
			GLHLM_FlashMode(1);
		}
		break;
		
		default:
		break;
	}	
}
#define READ_WHRITE_TIME_MAX    2
#define WHRITE_TIME_MAX    		2
static void iInitScConfig(void)
{
	uint8 i = 0;
	uint32 u32WaitStartTime = 0;
	
	int_disable_irq(INT_RX_EN_MASK);      	//关闭接收中断
	
	for(i=0; i<READ_WHRITE_TIME_MAX; i++)
	{
		u32WaitStartTime = GulSystickCount;
		while((hal_storage_256_read_bytes(SC_CONFIG_ADDR, (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)) != 0))
		{
			if(GulSystickCount - u32WaitStartTime > 1000)
			{
				goto PAGE2_START;
			}
		}
		
		if((s_sScConfig.u8CheckSum == iGetCheckSum((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)) && (s_sScConfig.u8CheckCrc == iGetCheckCrc((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)))
		{
			if((s_sScConfig.u8SaveFlag != FLASH_WRITE_FLAG))
			{
				s_sScConfig.u8SaveFlag = FLASH_WRITE_FLAG;
				iResetScConfig();
			}	
			break;
		}
	}
	if(i < READ_WHRITE_TIME_MAX)
	{
		goto READ_FLASH_END;
	}

PAGE2_START:	
	for(i=0; i<READ_WHRITE_TIME_MAX; i++)
	{
		u32WaitStartTime = GulSystickCount;
		while((hal_storage_256_read_bytes(SC_CONFIG_ADDR2, (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)) != 0))
		{
			if(GulSystickCount - u32WaitStartTime > 1000)
			{
				goto PAGE3_START;
			}
		}
		
		if((s_sScConfig.u8CheckSum == iGetCheckSum((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)) && (s_sScConfig.u8CheckCrc == iGetCheckCrc((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)))
		{
			if((s_sScConfig.u8SaveFlag != FLASH_WRITE_FLAG))
			{
				s_sScConfig.u8SaveFlag = FLASH_WRITE_FLAG;
				iResetScConfig();
			}	
			break;
		}
	}
	if(i < READ_WHRITE_TIME_MAX)
	{
		goto READ_FLASH_END;
	}
	
PAGE3_START:	
	for(i=0; i<READ_WHRITE_TIME_MAX; i++)
	{
		u32WaitStartTime = GulSystickCount;
		while((hal_storage_256_read_bytes(SC_CONFIG_ADDR3, (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)) != 0))
		{
			if(GulSystickCount - u32WaitStartTime > 1000)
			{
				goto PAGE2_START;
			}
		}
		
		if((s_sScConfig.u8CheckSum == iGetCheckSum((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)) && (s_sScConfig.u8CheckCrc == iGetCheckCrc((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2)))
		{
			if((s_sScConfig.u8SaveFlag != FLASH_WRITE_FLAG))
			{
				s_sScConfig.u8SaveFlag = FLASH_WRITE_FLAG;
				iResetScConfig();
			}	
			break;
		}
	}
	if(i < READ_WHRITE_TIME_MAX)
	{
		goto READ_FLASH_END;
	}
	
	s_sScConfig.u8SaveFlag = FLASH_WRITE_FLAG;
	iResetScConfig();
	
READ_FLASH_END:

	int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
    memcpy((uint8 *)(&s_PreScConfig), (uint8 *)(&s_sScConfig), sizeof(SC_CONFIG_S));  //解决每次上电都要保存一次数据的问题，防止快速通断电时，有时写数据导致FLASH数据出错。
	s_sScConfig.bSwitch = OFF;     //上电默认关机
	iRefreshFromConfig();
}

static void iSaveScConfigMainThread(void)
{
	uint8 i = 0;
	uint32 u32WaitStartTime = 0;

	if(GulSystickCount - s_u32PowerOnOffStartTime < ON_OFF_SAVE_INTERVER)
	{
		return;
	}   

    if(GulSystickCount - s_u32LastSaveTime >= SAVE_INTERVER)	//1s检查一次是否需要保存
	{
		s_u32LastSaveTime = GulSystickCount;
		
		if(0 == memcmp((u8 *)(&s_PreScConfig), (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)))
		{
			return;
		}

		s_sScConfig.u8CheckSum = iGetCheckSum((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2);
		s_sScConfig.u8CheckCrc = iGetCheckCrc((((uint8 *)(&s_sScConfig)) + 2), sizeof(SC_CONFIG_S) - 2);
			
		int_disable_irq(INT_RX_EN_MASK);      //关闭接收中断
		
		for(i=0; i<WHRITE_TIME_MAX; i++)
		{
			while(hal_storage_256_write_bytes(SC_CONFIG_ADDR, (u8 *)(&s_sScConfig), sizeof(SC_CONFIG_S)) != 0)
			{
				if(GulSystickCount - u32WaitStartTime > 100)
				{
					int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
					return;
				}
			}
			
			memset((uint8 *)&s_ScConfigTemp, 0x00, sizeof(SC_CONFIG_S));
			hal_storage_256_read_bytes(SC_CONFIG_ADDR, (uint8 *)&s_ScConfigTemp, sizeof(SC_CONFIG_S));
			if(0 == memcmp((u8 *)(&s_ScConfigTemp), (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)))
			{
				break;
			}
		}
		if(i >= WHRITE_TIME_MAX)    //没有写成功
		{
			int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
			return;
		}
		
		for(i=0; i<WHRITE_TIME_MAX; i++)
		{
			while(hal_storage_256_write_bytes(SC_CONFIG_ADDR2, (u8 *)(&s_sScConfig), sizeof(SC_CONFIG_S)) != 0)
			{
				if(GulSystickCount - u32WaitStartTime > 100)
				{
					int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
					return;
				}
			}
			
			memset((uint8 *)&s_ScConfigTemp, 0x00, sizeof(SC_CONFIG_S));
			hal_storage_256_read_bytes(SC_CONFIG_ADDR2, (uint8 *)&s_ScConfigTemp, sizeof(SC_CONFIG_S));
			if(0 == memcmp((u8 *)(&s_ScConfigTemp), (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)))
			{
				break;
			}
		}
		if(i >= WHRITE_TIME_MAX)    //没有写成功
		{
			int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
			return;
		}
		
		for(i=0; i<WHRITE_TIME_MAX; i++)
		{
			while(hal_storage_256_write_bytes(SC_CONFIG_ADDR3, (u8 *)(&s_sScConfig), sizeof(SC_CONFIG_S)) != 0)
			{
				if(GulSystickCount - u32WaitStartTime > 100)
				{
					int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
					return;
				}
			}
			
			memset((uint8 *)&s_ScConfigTemp, 0x00, sizeof(SC_CONFIG_S));
			hal_storage_256_read_bytes(SC_CONFIG_ADDR3, (uint8 *)&s_ScConfigTemp, sizeof(SC_CONFIG_S));
			if(0 == memcmp((u8 *)(&s_ScConfigTemp), (uint8 *)&s_sScConfig, sizeof(SC_CONFIG_S)))
			{
				break;
			}
		}
		if(i >= WHRITE_TIME_MAX)    //没有写成功
		{
			int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
			return;
		}
		
		int_enable_irq(INT_RX_EN_MASK);      //开启接收中断
		
		memcpy((u8 *)(&s_PreScConfig), (u8 *)(&s_sScConfig), sizeof(SC_CONFIG_S));
	}
}



void JTAPS_Init(void)
{
	GLHST_Init();
	GLHLM_Init();
	
	iInitScConfig();
}

void JTAPS_MainThread(void)
{
	iSaveScConfigMainThread();
	if(s_bIsRgbControledByPair)
	{
		iRgbPairFlashMainThread();
	}
	else
	{
		GLHLM_RenderProc();
	}
	
	if(s_sScConfig.eMode == E_MODE_TYPE_AUTO)
    {
		if(GLHLM_GetModeIsComplete() == TRUE)
		{
			s_eModeAuto++;
			if((uint8)s_eModeAuto >= (uint8)E_MODE_TYPE_AUTO)
			{
				s_eModeAuto = E_MODE_TYPE_FADE_RGB;
			}
			iAutoSlect(s_eModeAuto);
		}
	}
}


void JTIR_EventCb(void)
{

}

void GLHRF433_EventCb(void)
{
 
}

void GLHKEY_EventCb(uint8 u8Code, uint8 u8Action)
{

}


void GLHUART_RecCallback(uint8 u8Byte)
{
    
}

void app_dps_download(unsigned char dpid, unsigned char dpty, unsigned char dplen, unsigned char *dpvalue)
{

}

static void iRgbflash(uint8 u8Num, uint32 u32Time)
{
	if((u8Num == 0) || (u32Time == 0))
	{
		return;
	}
	
	s_u8PairFlashCnt = u8Num;
	s_u32PairFlashTime = u32Time;
	s_bIsRgbControledByPair = TRUE;
	s_u8PairFlashFlag = 0;
}

static void iRgbPairFlashMainThread(void)
{
	static uint32 s_u32Time = 0;
	
	if(s_bIsRgbControledByPair != TRUE)
	{
		return;
	}
	
	if(s_u8PairFlashCnt > 0)
	{
		if(s_u8PairFlashFlag == 0)
		{
			s_u8PairFlashFlag = 1;
			s_u32Time = GulSystickCount;
			
			GLHPWM_Set(PWM_CHANNEL_R, 0);
			GLHPWM_Set(PWM_CHANNEL_G, 0);
			GLHPWM_Set(PWM_CHANNEL_B, 0);
			GLHPWM_Set(PWM_CHANNEL_W, GLH_PWM_MAX);			
		}
		else if(s_u8PairFlashFlag == 1)
		{
			if(GulSystickCount - s_u32Time >= s_u32PairFlashTime)
			{
				s_u8PairFlashFlag = 2;
				s_u32Time = GulSystickCount;
				GLHPWM_Set(PWM_CHANNEL_R, 0);
				GLHPWM_Set(PWM_CHANNEL_G, 0);
				GLHPWM_Set(PWM_CHANNEL_B, 0);
				GLHPWM_Set(PWM_CHANNEL_W, 0);
			}
		}
		else if(s_u8PairFlashFlag == 2)
		{
			if(GulSystickCount - s_u32Time >= s_u32PairFlashTime)
			{
				s_u8PairFlashFlag = 0;
                s_u8PairFlashCnt--;
				if(s_u8PairFlashCnt == 0)
				{
					s_bIsRgbControledByPair = FALSE;
					iRefreshFromConfig();
				}
			}			
		}
	}
}

void RemoteReceiveCallback(str_ble_viot_para sReceiveData)
{
	uint8 i = 0, j = 0;
	static uint8 s_u8LastCount = 0x00;
	uint8 u8Rssi = 0;
	static BOOL s_bIsTestMode = FALSE;
	static uint8 s_u8Cnt = 0;
	static uint16 s_u16RssiTotal = 0;
	uint8 u8RssiAverage = 0;
	static uint32 u32LastOnTime = 0, u32LastOnLongTime = 0;   //上次接收到的短按开机键，长按开机键的时间
	static uint8 s_u8OnCnt = 0;     //短按开机键计数
	static BOOL s_bSpeedKeyPairFlag = FALSE;                  //处理按速度加键配对的问题。一次上电要么配对，要么清码，限制执行一个动作
	
	if(s_u8LastCount == sReceiveData.count)
	{
		return;
	}
	s_u8LastCount = sReceiveData.count;
	
    if(sReceiveData.addr == 0)             //工厂测试用的遥控器地址才可以为0
	{
		if(sReceiveData.cmd == E_BEACON_COM_TYPE_IS_NEED_PAIR)
		{
			u8Rssi = rf_get_rssi( 0 );
			#if LOG_MAJOR
			print("u8Rssi is %d\n", u8Rssi);
			#endif
			if(s_u8Cnt < RSSI_NUM)
			{
				s_u16RssiTotal += u8Rssi;
				s_u8Cnt++;
				if(s_u8Cnt == RSSI_NUM)
				{
					s_bIsTestMode = TRUE;
					if(sReceiveData.para[1] == NEED_PAIR)
					{
						s_sScConfig.bIsNeedPair = TRUE;
					}
					else if(sReceiveData.para[1] == NO_NEED_PAIR)
					{
						s_sScConfig.bIsNeedPair = FALSE;
					}
					
					GLHLM_SetSpeed(80);
					
					u8RssiAverage = s_u16RssiTotal / RSSI_NUM;
					if(u8RssiAverage >= FACTORY_TEST_RSSI_OK_VALUE)    //RSSI测试通过
					{
						GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0 , 0);
						GLHLM_SetColorBuff(1, 0, COLOUR_MAX, 0 , 0);
						GLHLM_SetColorBuff(2, 0, 0, COLOUR_MAX , 0);

						if(s_sScConfig.bIsNeedPair)
						{
							GLHLM_SetColorBuff(3, COLOUR_MAX, 0, 0 , COLOUR_MAX);
						}
						else
						{
							GLHLM_SetColorBuff(3, 0, COLOUR_MAX, 0 , COLOUR_MAX);						
						}
						GLHLM_JumpMode(4);
					}
					else
					{
						GLHLM_SetColorBuff(0, COLOUR_MAX, 0, 0, 0);
						GLHLM_FlashMode(1);						
					}
				}
			}
		}
	}
	
	if(s_bIsTestMode == TRUE)
	{
		return;
	}
	
	if(s_sScConfig.bIsNeedPair == TRUE)  //需要配对
	{
		if(GulSystickCount < 3000)      //开机时间在3s以内
		{
			if(sReceiveData.cmd == E_BEACON_COM_TYPE_SPEED_INC_LONG)
			{
				if(s_bSpeedKeyPairFlag == TRUE)   //本次上电已经因 E_BEACON_COM_TYPE_SPEED_INC_LONG 执行过一次配对或清码的动作
				{
					return;
				}
				
				for(i=0; i<ADDR_NUM_MAX; i++)
				{
					if((s_sScConfig.au32Addr[i] == sReceiveData.addr) && (s_sScConfig.au8Group[i] == sReceiveData.group_index))       //已经学习过
					{
                        for(j=i; j<(ADDR_NUM_MAX - 1); j++)
						{
							s_sScConfig.au32Addr[j] = s_sScConfig.au32Addr[j+1];
							s_sScConfig.au8Group[j] = s_sScConfig.au8Group[j+1];
						}
						s_sScConfig.au32Addr[ADDR_NUM_MAX - 1] = EMPTY_ADDR;
						s_sScConfig.au8Group[ADDR_NUM_MAX - 1] = EMPTY_ADDR;
						iRgbflash(3, 150);
						s_bSpeedKeyPairFlag = TRUE;
						return;
					}
				}
		
				for(i=0; i<ADDR_NUM_MAX; i++)
				{
					if(s_sScConfig.au32Addr[i] == EMPTY_ADDR)         //有空的位置，说明添加的遥控器数量没有满，直接添加就好了
					{
						s_sScConfig.au32Addr[i] = sReceiveData.addr;
						s_sScConfig.au8Group[i] = sReceiveData.group_index;
						iRgbflash(3, 500);
						s_bSpeedKeyPairFlag = TRUE;
						return;
					}
				}
		
				for(i=0; i<ADDR_NUM_MAX-1; i++ )                                        //位置都满了，把最前面的一个覆盖掉，后面的都向前移动一个位置
				{
					s_sScConfig.au32Addr[i] = s_sScConfig.au32Addr[i+1];
					s_sScConfig.au8Group[i] = s_sScConfig.au8Group[i+1];
 				}
				s_sScConfig.au32Addr[ADDR_NUM_MAX - 1] = sReceiveData.addr;             ////在最后面位置添加新学习到的ID
				s_sScConfig.au8Group[ADDR_NUM_MAX - 1] = sReceiveData.group_index;  
		
				iRgbflash(3, 500);
				s_bSpeedKeyPairFlag = TRUE;
				return;
			}
		}
		
		#if 0       //去掉遥控器开机键配对和清码的功能
		if(sReceiveData.cmd == E_BEACON_COM_TYPE_ON)
		{
			if(GulSystickCount - u32LastOnTime < 5000)
			{
				u32LastOnTime = GulSystickCount;
			
				s_u8OnCnt++;
				if(s_u8OnCnt == 5)
				{
					for(i=0; i<ADDR_NUM_MAX; i++)
					{
						if((s_sScConfig.au32Addr[i] == sReceiveData.addr) && (s_sScConfig.au8Group[i] == sReceiveData.group_index))       //已经学习过
						{
							for(j=i; j<(ADDR_NUM_MAX - 1); j++)
							{
								s_sScConfig.au32Addr[j] = s_sScConfig.au32Addr[j+1];
								s_sScConfig.au8Group[j] = s_sScConfig.au8Group[j+1];
							}
							s_sScConfig.au32Addr[ADDR_NUM_MAX - 1] = EMPTY_ADDR;
							s_sScConfig.au8Group[ADDR_NUM_MAX - 1] = EMPTY_ADDR;
							iRgbflash(3, 150);
							return;
						}
					}
				}
				if(s_u8OnCnt > 200)      //防止计满溢出
				{
					s_u8OnCnt = 200;
				}
			}
		}
		
		if(sReceiveData.cmd == E_BEACON_COM_TYPE_ON_LONG)    //长按开机键只能学习
		{
			if(GulSystickCount < 5000)
			{
				for(i=0; i<ADDR_NUM_MAX; i++)
				{
					if((s_sScConfig.au32Addr[i] == sReceiveData.addr) && (s_sScConfig.au8Group[i] == sReceiveData.group_index))       //已经学习过
					{
						return;
					}
				}
		
				for(i=0; i<ADDR_NUM_MAX; i++)
				{
					if(s_sScConfig.au32Addr[i] == EMPTY_ADDR)         //有空的位置，说明添加的遥控器数量没有满，直接添加就好了
					{
						s_sScConfig.au32Addr[i] = sReceiveData.addr;
						s_sScConfig.au8Group[i] = sReceiveData.group_index;
						iRgbflash(3, 500);
						return;
					}
				}
		
				for(i=0; i<ADDR_NUM_MAX-1; i++ )                                        //位置都满了，把最前面的一个覆盖掉，后面的都向前移动一个位置
				{
					s_sScConfig.au32Addr[i] = s_sScConfig.au32Addr[i+1];
					s_sScConfig.au8Group[i] = s_sScConfig.au8Group[i+1];
 				}
				s_sScConfig.au32Addr[ADDR_NUM_MAX - 1] = sReceiveData.addr;             ////在最后面位置添加新学习到的ID
				s_sScConfig.au8Group[ADDR_NUM_MAX - 1] = sReceiveData.group_index;  
		
				iRgbflash(3, 500);
				s_bSpeedKeyPairFlag = TRUE;
				return;
			}
		}
		#endif
	}
	
	if(s_sScConfig.bIsNeedPair == TRUE)  //需要配对
	{
		if(sReceiveData.addr == 0)
		{
			goto RECEIVE_HANDEL;
		}
		
		for(i=0; i<ADDR_NUM_MAX; i++)
		{
			if((sReceiveData.addr == s_sScConfig.au32Addr[i]) && ((sReceiveData.group_index == s_sScConfig.au8Group[i]) || (sReceiveData.group_index == 0)))    //接收到的ID已经配对过
			{
				goto RECEIVE_HANDEL;
			}
		}
		
		if(i >= ADDR_NUM_MAX)        //接收到的ID没有配对过
		{
			return;
		}
	}

RECEIVE_HANDEL:
    if(s_bIsRgbControledByPair)
	{
		s_bIsRgbControledByPair = FALSE;
		iRefreshFromConfig();
	}
	
	if((sReceiveData.cmd != E_BEACON_COM_TYPE_SPEED_INC) && (sReceiveData.cmd !=E_BEACON_COM_TYPE_SPEED_INC_LONG))   //上电检测到了非配对的按键，取消配对或清码
	{
		s_bSpeedKeyPairFlag = TRUE;  
	}
	
	switch(sReceiveData.cmd)
	{
		case E_BEACON_COM_TYPE_ON:
		{
			if(s_sScConfig.bSwitch == ON)
			{
				return;
			}
			s_sScConfig.bSwitch = ON;
			iRefreshFromConfig();
			
			s_u32PowerOnOffStartTime = GulSystickCount;
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_ON_LONG:
		{
			if(s_sScConfig.bSwitch == ON)
			{
				return;
			}
			s_sScConfig.bSwitch = ON;
			iRefreshFromConfig();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_OFF:
		{
			s_sScConfig.bSwitch = OFF;
			GLHLM_PowerOff();
			
			s_u32PowerOnOffStartTime = GulSystickCount;
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_OFF_LONG:
		{
			s_sScConfig.bSwitch = OFF;
			GLHLM_PowerOff();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_COLOR_MODE_BN_INC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.u8Bn >= APS_BN_MAX)
			{
				return;
			}
			
			if(s_sScConfig.u8Bn + APS_BN_CHANGE < APS_BN_MAX)
			{
				s_sScConfig.u8Bn += APS_BN_CHANGE;
			}
			else
			{
				s_sScConfig.u8Bn = APS_BN_MAX;
			}
			GLHLM_SetBn(s_sScConfig.u8Bn);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_COLOR_MODE_BN_INC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.u8Bn >= APS_BN_MAX)
			{
				return;
			}
			
			if(s_sScConfig.u8Bn + APS_BN_CHANGE < APS_BN_MAX)
			{
				s_sScConfig.u8Bn += APS_BN_CHANGE;
			}
			else
			{
				s_sScConfig.u8Bn = APS_BN_MAX;
			}
			GLHLM_SetBn(s_sScConfig.u8Bn);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_COLOR_MODE_BN_DEC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.u8Bn <= APS_BN_MIN)
			{
				return;
			}
			
			if(s_sScConfig.u8Bn - APS_BN_CHANGE > APS_BN_MIN)
			{
				s_sScConfig.u8Bn -= APS_BN_CHANGE;
			}
			else
			{
				s_sScConfig.u8Bn = APS_BN_MIN;
			}
			GLHLM_SetBn(s_sScConfig.u8Bn);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_COLOR_MODE_BN_DEC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.u8Bn <= APS_BN_MIN)
			{
				return;
			}
			
			if(s_sScConfig.u8Bn - APS_BN_CHANGE > APS_BN_MIN)
			{
				s_sScConfig.u8Bn -= APS_BN_CHANGE;
			}
			else
			{
				s_sScConfig.u8Bn = APS_BN_MIN;
			}
			GLHLM_SetBn(s_sScConfig.u8Bn);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;

		case E_BEACON_COM_TYPE_SPEED_INC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if((s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR) || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_WHITE)\
			    || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR_WHITE))       //静态模式下不去调速度
			{
				return; 
			}
			
			if(s_sScConfig.u8Speed + APS_SPEED_CHANGE < APS_SPEED_MAX)
			{
				s_sScConfig.u8Speed += APS_SPEED_CHANGE;
			}
			else
			{
				s_sScConfig.u8Speed = APS_SPEED_MAX;
			}
			GLHLM_SetSpeed(s_sScConfig.u8Speed);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_SPEED_INC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if((s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR) || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_WHITE)\
			    || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR_WHITE))       //静态模式下不去调速度
			{
				return; 
			}
			
			if(s_sScConfig.u8Speed + APS_SPEED_CHANGE < APS_SPEED_MAX)
			{
				s_sScConfig.u8Speed += APS_SPEED_CHANGE;
			}
			else
			{
				s_sScConfig.u8Speed = APS_SPEED_MAX;
			}
			GLHLM_SetSpeed(s_sScConfig.u8Speed);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_SPEED_DEC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if((s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR) || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_WHITE)\
			    || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR_WHITE))       //静态模式下不去调速度
			{
				return; 
			}
			
			if(s_sScConfig.u8Speed - APS_SPEED_CHANGE > APS_SPEED_MIN)
			{
				s_sScConfig.u8Speed -= APS_SPEED_CHANGE;
			}
			else
			{
				s_sScConfig.u8Speed = APS_SPEED_MIN;
			}
			GLHLM_SetSpeed(s_sScConfig.u8Speed);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_SPEED_DEC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if((s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR) || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_WHITE)\
			    || (s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR_WHITE))       //静态模式下不去调速度
			{
				return; 
			}
			
			if(s_sScConfig.u8Speed - APS_SPEED_CHANGE > APS_SPEED_MIN)
			{
				s_sScConfig.u8Speed -= APS_SPEED_CHANGE;
			}
			else
			{
				s_sScConfig.u8Speed = APS_SPEED_MIN;
			}
			GLHLM_SetSpeed(s_sScConfig.u8Speed);
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_MODE_INC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.eMode == E_MODE_TYPE_MAX - 1)   //已到达最大的模式
			{
				return;
			}
			
			if(s_sScConfig.eMode + 1 < E_MODE_TYPE_MAX)
			{
				s_sScConfig.eMode += 1;
			}
			else
			{
				s_sScConfig.eMode = E_MODE_TYPE_MAX - 1;
			}
			
			iRefreshFromConfig();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_MODE_INC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.eMode == E_MODE_TYPE_MAX - 1)   //已到达最大的模式
			{
				return;
			}
			
			if(s_sScConfig.eMode + 1 < E_MODE_TYPE_MAX)
			{
				s_sScConfig.eMode += 1;
			}
			else
			{
				s_sScConfig.eMode = E_MODE_TYPE_MAX - 1;
			}
			
			iRefreshFromConfig();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_MODE_DEC:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR)   //已到达最小的模式
			{
				return;
			}
			
			if(s_sScConfig.eMode - 1 > E_MODE_TYPE_STATIC_COLOUR)
			{
				s_sScConfig.eMode -=1;
			}
			else
			{
				s_sScConfig.eMode = E_MODE_TYPE_STATIC_COLOUR;
			}
			
			iRefreshFromConfig();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_MODE_DEC_LONG:
		{
            if(s_sScConfig.bSwitch != ON)
		    {
				return;
			}
			
			if(s_sScConfig.eMode == E_MODE_TYPE_STATIC_COLOUR)   //已到达最小的模式
			{
				return;
			}
			
			if(s_sScConfig.eMode - 1 > E_MODE_TYPE_STATIC_COLOUR)
			{
				s_sScConfig.eMode -=1;
			}
			else
			{
				s_sScConfig.eMode = E_MODE_TYPE_STATIC_COLOUR;
			}
			
			iRefreshFromConfig();
			
			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		case E_BEACON_COM_TYPE_COLOR_CIRCLE:
		{
            if(s_sScConfig.bSwitch != ON)
            {
				return;
			}	
		    iWheelTransRgb(sReceiveData.para[0], &s_sScConfig.au8Rgbw[R_INDEX], &s_sScConfig.au8Rgbw[G_INDEX], &s_sScConfig.au8Rgbw[B_INDEX]);
			s_sScConfig.eMode = E_MODE_TYPE_STATIC_COLOUR;
			iRefreshFromConfig();	

			s_u32LastSaveTime = GulSystickCount;
		}
		break;
		
		default:
		break;
	}
}

#if 0
static void iWheelTransRgb(const uint8 u8Wheel, uint8 *pu8R, uint8 *pu8G, uint8 *pu8B)
{
	if((u8Wheel >= R_MAX_POSITION_WHEEL_NUM) && (u8Wheel < B_MAX_POSITION_WHEEL_NUM))
	{
	    *pu8B = (u8Wheel - R_MAX_POSITION_WHEEL_NUM) * COLOUR_MAX / (B_MAX_POSITION_WHEEL_NUM - R_MAX_POSITION_WHEEL_NUM);
		*pu8R = COLOUR_MAX - *pu8B;
		*pu8G = 0;
	}
	else if((u8Wheel >= B_MAX_POSITION_WHEEL_NUM) && (u8Wheel < G_MAX_POSITION_WHEEL_NUM))
	{
	    *pu8G = (u8Wheel - B_MAX_POSITION_WHEEL_NUM)* COLOUR_MAX / (G_MAX_POSITION_WHEEL_NUM - B_MAX_POSITION_WHEEL_NUM);
		*pu8B = COLOUR_MAX - *pu8G;
		*pu8R = 0;	
	}
	else if((u8Wheel >= G_MAX_POSITION_WHEEL_NUM) && (u8Wheel <= WHEEL_NUM_MAX))
	{
	    *pu8R = (u8Wheel - G_MAX_POSITION_WHEEL_NUM)* COLOUR_MAX / (WHEEL_NUM_MAX - G_MAX_POSITION_WHEEL_NUM);
		*pu8G = COLOUR_MAX - *pu8R;
		*pu8B = 0;		
	}
}
#else
static void iWheelTransRgb(const uint8 u8Wheel, uint8 *pu8R, uint8 *pu8G, uint8 *pu8B)
{
	if(u8Wheel == R_MAX_POSITION_WHEEL_NUM)
	{
		*pu8R = COLOUR_MAX;
		*pu8G = 0;
		*pu8B = 0;		
	}
	else if(u8Wheel <= R_MAX_POSITION_WHEEL_NUM + ((B_MAX_POSITION_WHEEL_NUM - R_MAX_POSITION_WHEEL_NUM) >> 1))
	{
		*pu8R = COLOUR_MAX;
		*pu8G = 0;
		*pu8B = (u8Wheel - R_MAX_POSITION_WHEEL_NUM) * COLOUR_MAX / ((B_MAX_POSITION_WHEEL_NUM - R_MAX_POSITION_WHEEL_NUM) >> 1);
	}
	else if(u8Wheel < B_MAX_POSITION_WHEEL_NUM)
	{
		*pu8R = COLOUR_MAX - (u8Wheel - (R_MAX_POSITION_WHEEL_NUM + ((B_MAX_POSITION_WHEEL_NUM - R_MAX_POSITION_WHEEL_NUM) >> 1)))\
		        *COLOUR_MAX / ((B_MAX_POSITION_WHEEL_NUM - R_MAX_POSITION_WHEEL_NUM) >> 1);
		*pu8G = 0;	
		*pu8B = COLOUR_MAX;	
	}
	else if(u8Wheel == B_MAX_POSITION_WHEEL_NUM)
	{
		*pu8R = 0;
		*pu8G = 0;	
		*pu8B = COLOUR_MAX;	
	}
	else if(u8Wheel <= B_MAX_POSITION_WHEEL_NUM + ((G_MAX_POSITION_WHEEL_NUM - B_MAX_POSITION_WHEEL_NUM) >> 1))
	{
		*pu8R = 0;
		*pu8G = (u8Wheel - B_MAX_POSITION_WHEEL_NUM) * COLOUR_MAX / ((G_MAX_POSITION_WHEEL_NUM - B_MAX_POSITION_WHEEL_NUM) >> 1);		
		*pu8B = COLOUR_MAX;
	}
	else if(u8Wheel < G_MAX_POSITION_WHEEL_NUM)
	{
		*pu8R = 0;
		*pu8G = COLOUR_MAX;	
		*pu8B = COLOUR_MAX - (u8Wheel - (B_MAX_POSITION_WHEEL_NUM + ((G_MAX_POSITION_WHEEL_NUM - B_MAX_POSITION_WHEEL_NUM) >> 1)))\
		        *COLOUR_MAX / ((G_MAX_POSITION_WHEEL_NUM - B_MAX_POSITION_WHEEL_NUM) >> 1);
	}
	else if(u8Wheel == G_MAX_POSITION_WHEEL_NUM)
	{
		*pu8R = 0;
		*pu8G = COLOUR_MAX;	
		*pu8B = 0;			
	}
	else if(u8Wheel <= G_MAX_POSITION_WHEEL_NUM + ((WHEEL_NUM_MAX - G_MAX_POSITION_WHEEL_NUM) >> 1))
	{
		*pu8R = (u8Wheel - G_MAX_POSITION_WHEEL_NUM) * COLOUR_MAX / ((WHEEL_NUM_MAX - G_MAX_POSITION_WHEEL_NUM) >> 1);
		*pu8G = COLOUR_MAX;		
		*pu8B = 0;
	}
	else
	{
		*pu8R = COLOUR_MAX;
		*pu8G = COLOUR_MAX - (u8Wheel - (G_MAX_POSITION_WHEEL_NUM + ((WHEEL_NUM_MAX - G_MAX_POSITION_WHEEL_NUM) >> 1)))\
		        *COLOUR_MAX / ((WHEEL_NUM_MAX - G_MAX_POSITION_WHEEL_NUM) >> 1);
		*pu8B = 0;		
	}
}
#endif